import 'dart:io';

import 'package:FiduMart/core/network/RequestClient.dart';
import 'package:FiduMart/screen/home/home.dart';
import 'package:FiduMart/screen/home/login_view.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'login_util.dart';

class MemoController extends GetxController {
  var memos = <String>[].obs;

  var isShowed = false;

  @override
  void onInit() {
    super.onInit();
    loadMemos();
    if(isShowed){
      if(Platform.isAndroid){
        // googleLogin();
      }else{
        //todo ios
      }

    }
  }

  void addMemo(String memo) {
    memos.add(memo);
    saveMemos();
  }

  //google登录
  // Future<String?> googleLogin() async {
  //   var user = GoogleLoginUtil.currentUser();
  //   if (user != null) {
  //     print(user);
  //     await GoogleLoginUtil.signOut();
  //   }
  //   return await GoogleLoginUtil.signInWithGoogle();
  // }

  void loadMemos() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    List<String>? storedMemos = prefs.getStringList('memos');
    if (storedMemos != null) {
      memos.assignAll(storedMemos);
    }
  }

  void saveMemos() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setStringList('memos', memos);
  }

  void deleteMemo(int index) {
    memos.removeAt(index);
    saveMemos();
  }
}

class MemoPage extends StatelessWidget {
  final MemoController memoController = Get.put(MemoController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Memo'),
        backgroundColor: Colors.cyan,
        actions: [
          IconButton(
          icon: Icon(Icons.account_circle_rounded),
          onPressed: () {
            if (RequestClient.token.isEmpty) {
              Get.bottomSheet(isScrollControlled: true, LoginDialog());
              return;
            }else{
              showModalBottomSheet(
                  context: context,
                  backgroundColor: Colors.white,
                  builder: (BuildContext bc) {
                    return Container(
                      height: 170,
                      child: Padding(
                        padding: const EdgeInsets.all(20.0),
                        child: Column(
                          children: [
                            GestureDetector(

                              onTap: ()async{
                                await RequestClient().setToken("");
                                Get.offAll(HomePage());
                              },
                              child: Text('log out',style: TextStyle(color: Colors.black),),
                            ),
                            SizedBox(height: 10,),
                            Divider(color: Colors.black12,),
                            SizedBox(height: 10,),
                            GestureDetector(
                              onTap: ()async{
                                await RequestClient().setToken("");
                                Get.offAll(HomePage());
                              },
                              child:
                              Text('account cancellation',style: TextStyle(color: Colors.black),),
                            ),
                          ],
                        ),
                      ),
                    );
                  }
              );
            }
          },
        ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Obx(
          () => memoController.memos.length == 0
              ? Center(
                  child: Text(
                  'No memo added yet',
                  style: TextStyle(fontSize: 20,color: Colors.black),
                ))
              : ListView.builder(
                  itemCount: memoController.memos.length,
                  itemBuilder: (context, index) {
                    return Card(
                      elevation: 4,
                      color: Colors.cyan,
                      margin: const EdgeInsets.symmetric(vertical: 8.0),
                      child: ListTile(
                        title: Text(
                          memoController.memos[index],
                          style: TextStyle(fontSize: 18),
                        ),
                        onLongPress: () {
                          Get.dialog(
                            AlertDialog(
                              title: Text('Delete Memo',style: TextStyle(color: Colors.black)),
                              content: Text('Do you want to delete this memo?',style: TextStyle(color: Colors.black)),
                              actions: <Widget>[
                                TextButton(
                                  child: Text('Cancel'),
                                  onPressed: () {
                                    Navigator.of(context).pop();
                                  },
                                ),
                                TextButton(
                                  child: Text('Delete'),
                                  onPressed: () {
                                    memoController.deleteMemo(index);
                                    Navigator.of(context).pop();
                                  },
                                ),
                              ],
                            ),
                          );
                        },
                      ),
                    );
                  },
                ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _showAddMemoDialog(context),
        tooltip: 'Add Memo',
        child: Icon(
          Icons.add,
          color: Colors.black,
        ),
        backgroundColor: Colors.cyanAccent,
      ),
    );
  }

  void _showAddMemoDialog(BuildContext context) {
    TextEditingController _textFieldController = TextEditingController();

    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Add Memo', style: TextStyle(color: Colors.black)),
          content: TextField(
            controller: _textFieldController,
            style: TextStyle(color: Colors.black),
            decoration: InputDecoration(
                hintText: 'Enter memo',
                hintStyle: (TextStyle(color: Colors.grey))),
          ),
          actions: <Widget>[
            TextButton(
              child: Text('Cancel'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: Text('Save'),
              onPressed: () {
                if (_textFieldController.text.isEmpty) {
                  Fluttertoast.showToast(msg: 'Please enter memo');
                  return;
                }
                memoController.addMemo(_textFieldController.text);
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }
}
